# -*- coding: utf-8 -*-
import requests
from time import sleep
import user_agent_get
from urllib.parse import quote
from pyquery import PyQuery
import json
import pymysql



class splash_request:
    def __init__(self):
        self.pageCount=int(input("输入要爬取的页数："))
        lua_script = '''
        function main(splash, args)
          myarray={}
          assert(splash:go('https://www.jd.com/'))
          assert(splash:wait(1))
          html1= splash:html()
          input= splash:select("#key")
          input:send_text("python")
          submit= splash:select("#search > div > div.form > button")
          submit:mouse_click()
          splash:wait(2)
          local scroll_to = splash:jsfunc("window.scrollTo")
          scroll_to(0,100000)
          splash:wait(2)
          myarray[0]=splash:html()
          for i=1,%d,1
          do
            nextClick=splash:select("#J_bottomPage > span.p-num > a.pn-next")
            nextClick:mouse_click()
            splash:wait(2)
            local scroll_to = splash:jsfunc("window.scrollTo")
          	scroll_to(0,100000)
          	splash:wait(2)
            myarray[i]=splash:html()
           end
          return {
            html = myarray
          }
        end
        '''%(self.pageCount-1)
        splash_url = 'http://localhost:8050/execute?lua_source={}'.format(quote(lua_script))
        # 通过splash 获取响应
        response=requests.get(splash_url,headers={"user-agent":user_agent_get.user_agent_get()})
        response.encoding='utf-8'
        self.response_dict=json.loads(response.text)
        self.db=pymysql.connect(host="localhost",user='root',password="hlj010107",database='test',port=3306,charset='utf8')
        self.cursor=self.db.cursor()

    def dataAnalysis(self):
        for x in range(self.pageCount):
            mypy = PyQuery(self.response_dict['html']['{}'.format(x)])
            titleList = mypy('ul[class="gl-warp clearfix"] > li  div.p-name >a > em').items()
            priceList=mypy('ul[class="gl-warp clearfix"] > li  div.p-price > strong > i').items()
            imgUrlList=mypy('ul[class="gl-warp clearfix"] > li  div.p-img >a >img').items()
            for y in titleList:
                title=y.text().replace('\n'," ")
                price=next(priceList).text()
                imgUrl=next(imgUrlList).attr('src')
                print('title: ',title," price:",price," imgUrl:",imgUrl)
                self.sqlInsert(title,price,imgUrl)

    def sqlInsert(self,title,price,imgurl):
        sql='INSERT INTO jidongpaqu(title,price,imgUrl) VALUES("{}","{}","{}");'.format(title,price,imgurl)
        self.cursor.execute(sql)
        self.db.commit()

    def sqlClose(self):
        self.db.close()




if __name__ == '__main__':
    splash=splash_request()
    splash.dataAnalysis()
    splash.sqlClose()

